#include "loginwidget.h"
#include "ui_loginwidget.h"

using namespace std;

LoginWidget::LoginWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LoginWidget)
{
    ui->setupUi(this);
    QObject::connect(ui->pushButton_Login, SIGNAL(clicked()), this, SLOT(sinaStartLogin()));
    QObject::connect(ui->toolButton_App1,SIGNAL(clicked()),this,SLOT(rssRun()));
}

LoginWidget::~LoginWidget()
{
    delete ui;
}

void LoginWidget::sinaStartLogin(){
    if(ui->comboBox_ChoseClient->currentIndex() == 0)
    {
        this->pAccount = new Account;
        pAccount->setUsername(ui->lineEdit_Username->text());
        pAccount->setPassword(ui->lineEdit_Password->text());

        this->pSinaLogin = new SinaLogin(this->pAccount);
        connect(pSinaLogin,SIGNAL(loginSuccessed()),this,SLOT(sinaLoginSuccessed()));
        pSinaLogin->checkAuthStart();
    }
    else
    {
        this->prenrenAccount = new RenRenAccount;
        prenrenAccount->setUsername(ui->lineEdit_Username->text());
        prenrenAccount->setPassword(ui->lineEdit_Password->text());
        this->pOAuth = new OAuth2(*(this->prenrenAccount));
        //pOAuth->getAuthorizationCode();
        connect(this,SIGNAL(CheckRenrenLogin()),this,SLOT(renrenLoginSuccessed()));
        pOAuth->getAccessToken();
        pOAuth->getSessionKey();
        if(pOAuth->getAccount().getAccessToken()!=NULL)
        {
            this->prenrenAccount = &pOAuth->getAccount();
            emit CheckRenrenLogin();
        }
    }
}

Account* LoginWidget::getSinaAccount(){

    return this->pAccount;
}

void LoginWidget::rssRun()
{
    emit sendToMainWidget(Rss);
}

void LoginWidget::sinaLoginSuccessed(){

    emit sendToMainWidget(Sina);
}
void LoginWidget::renrenLoginSuccessed(){

    emit sendToMainWidget(RenRen);
}
RenRenAccount* LoginWidget::getRenRenAccount()
{
    return this->prenrenAccount;
}
